package com.xidian.zhiancloud.service.emergencyManagement;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.RevisionRecordMapper;
import com.xidian.zhiancloud.dao.SysOrganizationAbandonMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.dto.emergencyManagement.EmergencyPlanManagementDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.*;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.vo.chemicalsManagement.DangerousChemicalWarehouseOutVO;
import com.xidian.zhiancloud.vo.emergencyManagement.RevisionRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Summerless
 * @Date: 2021/2/23 20:41
 */

@Service
public class RevisionRecordService extends ServiceImpl<RevisionRecordMapper, RevisionRecord> implements IService<RevisionRecord> {
    @Autowired
    private RevisionRecordMapper mapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    public List<RevisionRecordVO> getList(Long id) {
        LambdaQueryWrapper<RevisionRecord> query = Wrappers.<RevisionRecord>lambdaQuery();
        query.eq(RevisionRecord::getEmergencyPlanId, id);
        List<RevisionRecord> list = mapper.selectList(query);
        List<RevisionRecordVO> ans = new LinkedList<>();
        list.forEach(t -> {
            RevisionRecordVO vo = new RevisionRecordVO();
            BeanUtils.copyProperties(t, vo);
            Organization organization = organizationMapper.selectById(vo.getOrganizationId());
            vo.setDepartmentName(organization == null ? "该部门已被删除" : organization.getName());
            String[] split = vo.getReviserIds().split(",");
            vo.setRevisiorNames(Arrays.stream(split).map(ids_s -> {
                SysUser tmp = sysUserMapper.selectById(ids_s);
                return tmp == null ? "该用户已被删除" : tmp.getName();
            }).collect(Collectors.toList()));
            ans.add(vo);
        });
        return ans;
    }



}
